﻿@{
    ViewBag.Title = "成品库储备定额";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<script>
    var currRowId = 0;
    var status = 'I';
    $(function () {
        initControl();
        gridList();
        hideEditDiv();
    });
    function hideEditDiv() {
        $('#eidt_div').hide();
        $('.ui-jqgrid-bdiv').height($('.ui-jqgrid-bdiv').height() + 58);
        if (currRowId > 0) {
            if ($('#btn_edit').hasClass("disabled")) {
                $('#btn_edit').removeClass("disabled");
            }
            if ($('#btn_remove').hasClass("disabled")) {
                $('#btn_remove').removeClass("disabled");
            }
        }
        status = 'I'
        $('#btn_list').show();
    }
    function showEditDiv() {
        $('#eidt_div').show();
        $('.ui-jqgrid-bdiv').height($('.ui-jqgrid-bdiv').height() - 58);
        if (!$('#btn_edit').hasClass("disabled")) {
            $('#btn_edit').addClass("disabled");
        }
        if (!$('#btn_remove').hasClass("disabled")) {
            $('#btn_remove').addClass("disabled");
        }
        status = "U";
        $('#btn_list').hide();
    }
    function initControl() {
        initBindSelect("#form1");
        initBindSelect("#form2");
    }
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/MainData/StoProReservior/GetStoProReserviorCPJson",
            height: $(window).height() - 176,
            colModel: [
                {
                    label: '<a id="grid_chb_selectAll" onclick="grid_chb_selectAll()">全选</a>', name: '', width: 40, align: 'center', sortable: false, frozen: true, formatter: function (cellvalue, options, rowObject) {
                        return '<input class="grid_chb_select" value="' + rowObject.ReserveID + '" name="checkbox" type="checkbox" onclick="" style="vertical-align:middle">';
                    }
                },
                { label: '零件编号', name: 'FProductionNO', width: 120, align: 'left', sortable: false, frozen: true },
                { label: '零件名称', name: 'FProductionName', width: 200, align: 'left', sortable: false, frozen: true },
                { label: '制造单位', name: 'FProCompanyNO', width: 120, align: 'left', sortable: false },
                { label: '制造单位名称', name: 'FProCompanyName', width: 200, align: 'left', sortable: false },
                { label: '最低储备', name: 'FLowestStore', width: 80, align: 'right', sortable: false },
                { label: '最高储备', name: 'FHighestStore', width: 80, align: 'right', sortable: false },
                { label: '安全储备', name: 'FSafetystore', width: 80, align: 'right', sortable: false },
                { label: '计算储备', name: 'FCalcuStore', width: 80, align: 'right', sortable: false },
                { label: '储备天数', name: 'FStoreDates', width: 80, align: 'right', sortable: false },
                { label: '日均消耗', name: 'FDailyConsume', width: 80, align: 'right', sortable: false },
                { label: '计算日期', name: 'FCalDate', width: 80, align: 'center', sortable: false },
                { label: '零件分类', name: 'FProductionTypeName', width: 80, align: 'center', sortable: false },
                {
                    label: '当前状态', name: 'FMaintainStatus', width: 80, align: 'left', sortable: false, formatter: function (cellvalue) {
                        return cellvalue == "1" ? "已发布" : cellvalue == "0" ? "未发布" : "";
                    }
                },
                { label: '操作人员', name: 'FUpdateBy', width: 80, align: 'left', sortable: false },
                { label: '输入日期', name: 'FUpdateDate', width: 80, align: 'center', sortable: false },
                { label: '输入时间', name: 'FUpdateTime', width: 80, align: 'center', sortable: false },
                { label: '发布人员', name: 'FAuditingBy', width: 80, align: 'left', sortable: false },
                { label: '发布日期', name: 'FAuditingDate', width: 80, align: 'center', sortable: false },
                { label: '发布时间', name: 'FAuditingTime', width: 80, align: 'center', sortable: false },
                { label: '零件分类(隐藏)', name: 'FProductionTypeNO', width: 80, align: 'center', hidden: true }

            ],
            pager: "#gridPager",
            sortname: 'FProductionNO ASC,FProCompanyNO ASC',
            viewrecords: true,  //是否显示行数
            rownumWidth: 40,
            gridComplete: function () {

            },
            loadComplete: function () {
                var rowDatas = $gridList.jqGrid("getRowData");
                var length = rowDatas.length + 1;
                if (length > 0) {
                    if (currRowId == 0) {
                        currRowId = 1;
                    } else if (currRowId > length) {
                        currRowId = length;
                    }
                }
                if (currRowId > 0) {
                    $gridList.jqGrid('setSelection', currRowId);
                    var $grid = $('#gview_gridList .ui-jqgrid-bdiv');
                    $grid.setScrollTop(length, currRowId);
                }
            },
            onSelectRow: function (rowid) {
                currRowId = rowid;
                var rowData = $("#gridList").jqGrid('getRowData', currRowId);
                if ($('#btn_remove').hasClass("disabled")) {
                    $('#btn_remove').removeClass("disabled");
                }
                if ($('#btn_edit').hasClass("disabled")) {
                    $('#btn_edit').removeClass("disabled");
                }

                emptyDataById("#eidt_div");
                if (status == "U") {
                    setDataByJson("#eidt_div", rowData);
                    $('#FProCompany').val(rowData.FProCompanyNO + ' ' + rowData.FProCompanyName);
                    initBindSelect("#eidt_div");
                }

            },
            beforeSelectRow: function (rowid, e) {
                
            },
            onPaging: function (page_btn) {
                currRowId = 0;
            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        $gridList.jqGrid('setFrozenColumns');
        $("#btn_search").click(function () {
            reloadGrid();
        });
    }
    function reloadGrid() {
        $("#gridList").jqGrid('setGridParam', {
            postData: $("#form1").formSerialize(),
        }).trigger('reloadGrid');
    }
    function btn_edit() {
        var rowData = $("#gridList").jqGrid('getRowData', currRowId);
        emptyDataById("#eidt_div");
        setDataByJson("#eidt_div", rowData);
        $('#FProCompany').val(rowData.FProCompanyNO + ' ' + rowData.FProCompanyName);
        initBindSelect("#eidt_div");
        showEditDiv();
    }
    function btn_remove() {
        var rowData = $("#gridList").jqGrid('getRowData', currRowId);
        $.modalConfirm("注：您确定要【清除】该条数据吗？", function (r) {
            if (r) {
                rowData["FStoreNO"] = '01';
                $.submitForm({
                    url: "/MainData/StoProReservior/SubmitStoProReserviorForm?status=D",
                    param: rowData,
                    success: function (data) {
                        emptyDataById("#form2");
                        reloadGrid();
                    }
                })
            }
        });
    }
    function btn_publish() {
        var chk_value = [];
        $('input[type="checkbox"].grid_chb_select:checked').each(function () {
            chk_value.push($(this).val());
        });
        if (chk_value.length <= 0) {
            $.modalAlert("注：您没有勾选任何数据。", "warning");
        } else {
            //alert(chk_value.join(','));
            $.modalConfirm("注：您确定要【发布】勾选的数据吗？", function (r) {
                if (r) {
                    $.submitForm({
                        url: "/MainData/StoProReservior/SubmitStoProReserviorStatus",
                        param: { keyValue: chk_value.join(',') },
                        success: function () {
                            reloadGrid();
                        }
                    })
                }
            });
        }

    }
    function btn_ok() {
        submitForm();
    }
    function submitForm() {
        if (!$('#form2').formValid()) {
            return false;
        }
        var data = $("#form2").formSerialize();
        data["FStoreNO"] = '01';
        $.submitForm({
            url: "/MainData/StoProReservior/SubmitStoProReserviorForm?status=" + status,
            param: data,
            success: function (result) {
                reloadGrid();
                hideEditDiv();
            }
        })
    }
    function btn_cancel() {
        hideEditDiv();
    }
    function grid_chb_selectAll() {
        var txt = $('#grid_chb_selectAll').text();
        alert(txt);
        if (txt == "全选") {
            $('.grid_chb_select').prop("checked", true);
            $('#grid_chb_selectAll').text("反选");
        } else {
            $('.grid_chb_select').prop("checked", false);
            $('#grid_chb_selectAll').text("全选");
        }

    }
    function btn_export() {
        var url = "/Common/ExcelExport?" + encodeURI(init_sql());
        location.href = url;
    }
    function init_sql() {
        var tablename = "V_CPK_81_CB_ReserveDevice";
        var condition = "UseUnit='@FriendStar.Code.Configs.GetValue("UseUnit")'";
        var cols = [];
        //获取列字段
        var colModel = $("#gridList").jqGrid('getGridParam', 'colModel');
        for (var i = 0; i < colModel.length; i++) {
            var columnHidden = colModel[i].hidden;
            var columnName = colModel[i].name;
            var columnLabel = colModel[i].label;
            if (columnHidden == false && columnName != "rn" && columnName != "") {
                if (columnLabel != '') {
                    cols.push(columnName + ' [' + columnLabel.replace(/<[^>]+>/g, "") + ']');
                } else {
                    cols.push(columnName);
                }
            }
        }
        if ($('#FProductionNO1').val() != '') {
            condition += " AND FProductionNO LIKE '%" + $('#FProductionNO1').val() + "%'";
        }
        if ($('#FProductionName').val() != '') {
            condition += " AND FProductionName LIKE '%" + $('#FProductionName').val() + "%'";
        }
        if ($('#FProCompanyNO1').val() != '') {
            condition += " AND FProCompanyNO = '" + $('#FProCompanyNO1').val() + "'";
        }
        var sql = "SELECT " + cols.join(',') + " FROM " + tablename + " WHERE " + condition + " ORDER BY FProductionNO ASC,FProCompanyNO ASC";
        return "sql=" + sql;
    }
</script>
<div id="layout" style="height: 100%; width: 100%;">
    <form id="form1">
        <div style="padding: 12px 0 5px 0; padding-left: 15px; border-bottom: 1px solid #ccc;" class="line-table input-width-140">
            <div class="btn-group" style="margin-top:-9px;">
                零件编号<br /><input id="FProductionNO1" name="FProductionNO" type="text" class="form-control  input-width-105" /> &nbsp;
            </div>   
            <div class="btn-group" style="margin-top:-9px;">
                零件名称<br /><input id="FProductionName" name="FProductionName" type="text" class="form-control" /> &nbsp;
            </div>                     
            <div class="btn-group input-width-210" style="margin-top:-9px;">
                制造单位<br />
                <select id="FProCompanyNO1" name="FProCompanyNO" class="form-control">
                    <option value="">&nbsp;</option>
                    @foreach (var item in ViewBag.fproCompany)
                    {
                        <option value="@item.UnitNO">@item.UnitNO @item.UnitShortName</option>
                    }
                </select>
                &nbsp;
            </div>
            <div class="btn-group" style="">
                <a id="btn_search" class="btn btn-primary center-block" onclick=""><span class="glyphicon glyphicon-search"></span> 检索</a>
            </div>
            <div id="btn_list" class="btn-group" style="float:right;">
                <a id="" class="btn btn-primary center-block" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span> 刷新</a>
                <a id="btn_edit" class="btn btn-primary center-block disabled" onclick="btn_edit()"><span class="glyphicon glyphicon-edit"></span> 输入</a>
                <a id="btn_remove" class="btn btn-primary center-block disabled" onclick="btn_remove()"><span class="glyphicon glyphicon-trash"></span> 清除</a>
                <a id="btn_publish" class="btn btn-primary center-block" onclick="btn_publish()"><span class="glyphicon glyphicon-pencil"></span> 发布</a>
                <a id="btn_export" class="btn btn-primary center-block" onclick="btn_export()"><span class="glyphicon glyphicon-download"></span> 导出</a>
            </div>
        </div>
    </form>
    <form id="form2">
        <table id="eidt_div" class="form" style="margin:3px 0;">
            <tr>
                <th class="formTitle" style="width:90px;">零件编号</th>
                <td class="formValue input-width-185" style="width:230px;">
                    <input id="FProductionNO" name="FProductionNO" type="text" class="form-control required" readonly="readonly" />
                </td>
                <td class="formValue" style="width:135px;">
                    <div style="position:absolute;bottom:0;">最低存储：</div>
                </td>
                <td class="formValue" style="width:135px;">
                    <div style="position:absolute;bottom:0;">最高储备：</div>
                </td>
                <td class="formValue" style="width:135px;">
                    <div style="position:absolute;bottom:0;">安全储备：</div>
                </td>
                <td class="formValue" style="width:135px;">
                    <div style="position:absolute;bottom:0;">储备天数：</div>
                </td>
                <td class="formValue" style="width:135px;">
                    <div style="position:absolute;bottom:0;">计算存储：</div>
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th class="formTitle">供货单位</th>
                <td class="formValue input-width-210">
                    <input id="FProCompany" name="FProCompany" type="text" class="form-control" readonly="readonly" />
                    <input id="FProCompanyNO" name="FProCompanyNO" type="hidden" />
                </td>
                <td class="formValue" style="position:relative">
                    <input id="FLowestStore" name="FLowestStore" type="text" class="form-control input-width-105 text-right digits" />
                </td>
                <td class="formValue">
                    <input id="FHighestStore" name="FHighestStore" type="text" class="form-control input-width-105 text-right digits" />
                </td>
                <td class="formValue">
                    <input id="FSafetystore" name="FSafetystore" type="text" class="form-control input-width-105 text-right digits" />
                </td>
                <td class="formValue">
                    <input id="FStoreDates" name="FStoreDates" type="text" class="form-control input-width-105 text-right digits" />
                </td>
                <td class="formValue">
                    <input id="FCalcuStore" name="FCalcuStore" type="text" class="form-control input-width-105 text-right digits" />
                </td>
                <td class="formValue">
                    &nbsp;&nbsp;&nbsp;
                    <div class="btn-group" style="margin-top:2px;">
                        <a id="btn_ok" class="btn btn-primary center-block btn-1" onclick="btn_ok()"><span class="glyphicon glyphicon-ok"></span> 输入</a>
                    </div>
                    &nbsp;&nbsp;&nbsp;
                    <div class="btn-group" style="margin-top:2px;">
                        <a id="btn_cancel" class="btn btn-primary center-block btn-1" onclick="btn_cancel()"><span class="glyphicon glyphicon-remove"></span> 取消</a>
                    </div>
                </td>
            </tr>
        </table>
    </form>
    <div style="">
        <table id="gridList"></table>
        <div id="gridPager"></div>
    </div>
</div>

